﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpUtils
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //ListSortTest();
                TimeStampTest();
                //LinqExTest();

                Console.WriteLine("请按任意键继续..");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Console.ReadKey();
            }
        }

        static void TimeStampTest()
        {
            DateTime timeNow = DateTime.Now;
            Console.WriteLine("new timestamp1:");
            var timeStamp1 = new DateTimeOffset(timeNow).ToUnixTimeSeconds();
            Console.WriteLine(timeStamp1);
            Console.WriteLine("new timestamp2:");
            var timeStamp2 = new DateTimeOffset(timeNow).ToUnixTimeMilliseconds();
            Console.WriteLine(timeStamp2);
            Console.WriteLine("timestamp1 to DateTime:");
            Console.WriteLine(DateTimeOffset.FromUnixTimeSeconds(timeStamp1));
            Console.WriteLine("timestamp2 to DateTime:");
            Console.WriteLine(DateTimeOffset.FromUnixTimeMilliseconds(timeStamp2));
        }

        static void LinqExTest()
        {
            var list = new List<Student>();
            list.Add(new Student(1, "张三"));
            list.Add(new Student(1, "张三"));
            list.Add(new Student(3, "王五"));
            list.Add(new Student(2, "李四"));

            Console.WriteLine("去重（默认）：");
            list = list.Distinct().ToList();
            foreach (var item in list)
            {
                Console.WriteLine(item.Id + "," + item.Name);
            }
            Console.WriteLine("去重（改进）：");
            list = list.Distinct(a => a.Id).ToList();
            foreach (var item in list)
            {
                Console.WriteLine(item.Id + "," + item.Name);
            }

            Console.WriteLine("包含{2,李四}（默认）：");
            bool flag = list.Contains(new Student(2, "李四"));
            Console.WriteLine(flag);
            Console.WriteLine("包含{2,李四}（改进）：");
            flag = list.Contains(new Student(2, "李四"), new CommonEqualityComparer<Student, int>(a => a.Id));
            Console.WriteLine(flag);
        }

        static void ListSortTest()
        {
            var stopwatch = new System.Diagnostics.Stopwatch();
            var list = new List<Student>();
            for (int i = 0; i < 10000; i++)
            {
                list.Add(new Student(2, "张三"));
                list.Add(new Student(5, "张三"));
                list.Add(new Student(1, "张三"));
                list.Add(new Student(4, "王五"));
                list.Add(new Student(3, "李四"));
            }

            Console.WriteLine("New Comparison Sort（Name Asc）：");
            var list6 = list.ToList();//Copy
            stopwatch.Restart();
            list6.Sort((x, y) => string.Compare(x.Name, y.Name));
            Console.WriteLine("耗时：" + stopwatch.ElapsedMilliseconds + "ms");
            Console.WriteLine();

            Console.WriteLine("New Comparison Sort（Name Desc then Id Asc）：");
            var list7 = list.ToList();//Copy
            stopwatch.Restart();
            list7.Sort((x, y) =>
            {
                int result = string.Compare(y.Name, x.Name);
                if (result == 0)
                {
                    result = x.Id.CompareTo(y.Id);
                }
                return result;
            });
            Console.WriteLine("耗时：" + stopwatch.ElapsedMilliseconds + "ms");
            Console.WriteLine();
        }
    }
}
